package pkg.networking;
/**
 * BomberBoy, CPS240 Term Project - Team A
 * Andrew Osterink, David Bash, Scott Kidder
 * 
 * Listens for commands and if there not remote broadcasts them
 * to the network
 *
 */

import pkg.commands.*;

public class RemoteDistributor implements ICommandExecutor,INetworkCallback {
	
	ICommandExecListener listener;
	ClientConnection client;
	public RemoteDistributor(ClientConnection client)
	{
		this.client = client;
		client.registerListener(this);
	}

	@Override
	public void setListener(ICommandExecListener listener) {
		this.listener = listener;
	}

	@Override
	public void execute(ICommand command) 
	{
		if(RemoteCommand.isRemoteCommand(command))
		{
			RemoteCommand remote = (RemoteCommand)command;
			
			if(remote.getUserId() != client.getUserId() && remote.getUserId() != -1)
			{
				//stub might need to put stuff hear later
			}
		}
		else
			client.sendMessage(new RemoteCommand(command,client.getUserId()));
	}
	
	private void notify(ICommand cmd)
	{
		//if the user that send the message is us don't reprocess it, just return
		if(cmd instanceof RemoteCommand && ((RemoteCommand)cmd).getUserId() == client.getUserId())
			return;
			listener.receive(cmd);
	}

	@Override
	public void newtworkEvent(CallbackEvent evnt, Object obj) 
	{
		if(evnt == CallbackEvent.MessageReceived)
			notify((ICommand) obj);
		
	}

}
